#include "CH552.h"
#include "Debug.h"
#include "LT8920.H"
#include "Gpio.h"
#include "Spi.h"
#include "intrins.h"
#include "types.h"
#define SYNCWORDH 0xA3
#define SYNCWORDL 0x80
#define LT8920_SetTX(chn) SPI_W2Byte(7,0x01,chn)
#define LT8920_SetRX(chn) SPI_W2Byte(7,0x00,(chn|0x80))
#define LT8920_EntIDel()  SPI_W2Byte(7,0x00,globalChn)
static char globalChn = 0x00;
xdata LT8920_ConfigReg LT8920_reg[] =
    {
        {0, 0x6f, 0xe0}, //-- 6fe0(Recommand)(6fef = default)
        {1, 0x56, 0x81}, 
        {2, 0x66, 0x17},
        {4, 0x9c, 0xc9},
        {5, 0x66, 0x37},
        {7, 0x00, 0x30}, //channel is 2402Mhz
        {8, 0x6c, 0x90},
        {9, 0x48, 0x00}, //PA -12.2dbm,1840?
        {10, 0x7f, 0xfd},
        {11, 0x00, 0x08},
        {12, 0x00, 0x00},
        {13, 0x48, 0xbd},
        {22, 0x00, 0xff},
        {23, 0x80, 0x05},
        {24, 0x00, 0x67},
        {25, 0x16, 0x59},
        {26, 0x19, 0xe0},
        {27, 0x13, 0x00},
        {28, 0x18, 0x00},
        {32, 0x48, 0x00}, //tail 3 byte,sync word 32bit(REG36 REG39) 0100 1000 0000 0000
        {33, 0x3f, 0xc7},
        {34, 0x20, 0x00},
        {35, 0x04, 0x00}, //-- ReSend Timer=10?4?
        /*sync word 设置为32bit 时，使用 Reg[39]和Reg[36]*/
        {36, SYNCWORDH, SYNCWORDL},
        {37, 0xA3, 0x80},
        {38, 0xA5, 0x5A},
        {39, 0xA3, 0x80},
        
        {40, 0x44, 0x03},
        {41, 0xBC, 0x00}, /*CRC is ON, scramble is OFF, AUTO_ACK  Enable, PKT Low Active*/
        {42, 0xFD, 0xb0}, /*??RX_ACK?? 176us*/
        {43, 0x00, 0x0f},
        {44, 0x04, 0x00},//{44, 0x01, 0x00}, //250Kbps
        {45, 0x05, 0x52},
        {50, 0x00, 0x00}};
        
#define RegSize (sizeof(LT8920_reg) / sizeof(LT8920_ConfigReg))
sbit RST = P3 ^ 4;
sbit PKT = P3 ^ 2; //pkt 脚要再看看文档
void LT8920_init(void)
{
    int ii = 0;
    LT8920_ConfigReg *regPtr;
    SPI_Init();
    Port3Cfg(1, 4); //设置管脚RST管脚
    RST = 0;
    mDelaymS(100);
    RST = 1;
    mDelaymS(500);
    for (ii = 0; ii < RegSize; ii++)
    {
        regPtr = &LT8920_reg[ii];
        SPI_W2Byte(regPtr->reg_addr, regPtr->reg_data_h, regPtr->reg_data_l);
    }
    LT8920_SetRX(globalChn);
    Port3Cfg(0, 2); //设置管脚PKT管脚
    //TODO 打开PKT脚中断
}
#if 0
UINT16 LT8920_regTest(UINT8 address)
{
    UINT8 reg[2];
    SPI_R2Byte(address, reg);
    return *((UINT16 *)(&reg[0]));
}
#endif

void LT8920_WFIFO(UINT8 *buf,UINT8 len)
{
    //UINT8 ii = 0;
    SPI_MultStart();
    SPI_MultRWByte(50);//写入fifo地址
    if(len>63)
    {
        len = 63;
    }

    SPI_MultRWByte(len);

    while(len--)
    {
        SPI_MultRWByte(*buf);
        buf++;
    }
    SPI_MultStop();
}

UINT8 LT8920_RFIFO(UINT8 *buf,UINT8 bufLen)
{
    unsigned char ret;
    SPI_MultStart();
    SPI_MultRWByte(0xB2);// = 50 | 0x80
    ret = SPI_MultRWByte(0xff);

    if(bufLen> 63)
    {
        bufLen = 63;
    }

    if(bufLen>ret)
    {
        bufLen = ret;
    }
    
    ret = bufLen;
    while(bufLen--)
    {
        *buf = SPI_MultRWByte(0xff);
         buf++;
    }

    SPI_MultStop();

    return ret;
}

void LT8920_ClearFifo()
{
    SPI_W2Byte(52,0x80,0x80);
} 

void LT8920_TXPKT(UINT8 *buf, UINT8 len)
{
    UINT8 ii = 0;
    LT8920_EntIDel();
    SPI_W2Byte(52,0x80,0x80);
    LT8920_WFIFO(buf,len);
    mDelayuS(10);
    LT8920_SetTX(globalChn);
    mDelayuS(10);
    while(PKT)
    {
        ii++;
        mDelayuS(1000);
        if(ii>30)
        {
            break;
        }
    }
}

UINT8 LT8920_isRxArrival(void)
{
    return (PKT == 0);
}


UINT8 LT8920_RXPKT(UINT8 *buf,UINT8 bufLen)
{
    UINT8 ret;
    LT8920_EntIDel(); 
    mDelayuS(10);
    ret = LT8920_RFIFO(buf,bufLen);
    LT8920_ClearFifo();
    LT8920_SetRX(globalChn);
    mDelayuS(10);
    return ret;
}
//0 ok 1 failed
void LT8920_SetChn(UINT8 chn)
{
    LT8920_EntIDel(); 
    mDelayuS(10);
    LT8920_ClearFifo();
    globalChn = chn;
    
}
void  LT8920_SetToRx(void)
{
    LT8920_EntIDel();
    LT8920_ClearFifo();
    LT8920_SetRX(globalChn);
    mDelayuS(10);
}
/*UID是16bit的，所以我们只设置36寄存器*/
void LT8920_SetSyncword(unsigned char syncword[2])
{
    LT8920_EntIDel();
    mDelayuS(10);
    SPI_W2Byte(36,syncword[1],syncword[0]);
    LT8920_EntIDel();
    mDelayuS(10);
    LT8920_SetRX(globalChn);
    mDelayuS(10);
}

/*UID是16bit的，所以我们只设置36寄存器*/
void LT8920_SetSyncwordDefault(void)
{
    LT8920_EntIDel();
    mDelayuS(10);
    SPI_W2Byte(36,SYNCWORDH, SYNCWORDL);//默认同步字
    LT8920_EntIDel();
    mDelayuS(10);
    LT8920_SetRX(globalChn);
    mDelayuS(10);
}

void LT8920_INTERRUPT(void) interrupt INT_NO_INT0 //PKT中断
{

}